<?php
/**
 * 飞信机器人--功能插件--心理测试
 * 功能：实现心理测试类
 * 数据表：fx_mindtest_questions,fx_mindtest_options
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
/*
支持参数(fx_menus表 extra_params字段)：
classid=分类id
*/
/*
 数据表定义:
主题表：
CREATE TABLE `fx_mindtest_questions` (
  `id` int(14) NOT NULL auto_increment,
  `class_id` int(8) NOT NULL,
  `title` text,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

选项及测试结果表：
CREATE TABLE `fx_mindtest_options` (
  `id` int(14) NOT NULL auto_increment,
  `question_id` int(14) NOT NULL,
  `title` varchar(256) NOT NULL default '',
  `answer` text,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
*/

class MindTest extends CommonPlugin
{
	var $UUID='d6ebad7c-08f0-4d4c-940e-5b66982f8cf6';
	
	public function MindTest(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
	
	private function setQuestionID($class_id,$question_id)
	{
		$this->session->set("MindTest{$class_id}_qid", $question_id);
	}

	private function getQuestionID($class_id)
	{
		$id = $this->session->getValue("MindTest{$class_id}_qid");
		return $id;
	}
	
	private function setPage($class_id,$page)
	{
		$this->session->set("MindTest{$class_id}_page", $page);
	}

	private function getPage($class_id)
	{
		$id = $this->session->getValue("MindTest{$class_id}_page");
		return $id;
	}	
	
	public function getListContent($class_id,$count,$input)
	{
		$msg = "<Font Face='楷体' Color='-16744320' Size='10.5'>";
		$extra = "";
		$perPage = 4;
		$pages = 1;
		$currentPage = $this->getPage($class_id);
		if($count>$perPage)
		{
			$pages = ceil($count/$perPage);
			$extra = ",(第{$currentPage}页 共{$pages}页)p-上一页 n-下一页";
			$start = $perPage * ($currentPage-1);
			$sql = "select * from fx_mindtest_questions where class_id=$class_id order by id desc limit $start, $perPage";
		}else 
		{
			$sql = "select * from fx_mindtest_questions where class_id=$class_id order by id desc";
		}
		$retArr = $this->session->db->fetchQueryAll($sql);

		$found=false;
		$id = 0;
		for($i=0; $i<count($retArr); $i++ )
		{
			if( intval($input) == $i+1 )
			{
				$msg.=$retArr[$i]['title'];
				$msg.="\n";
				$id = $retArr[$i]['id'];
				$found = true;
				$this->setQuestionID($class_id, $id);
				break;
			}
		}
		
		$extr = "";
		if(!$found)
		{
			$msg.="您的输入有误";
			$extr = ",l-返回列表";
		}else 
		{
			$sql = "select * from fx_mindtest_options where question_id={$id} order by id asc";
			$retArr = $this->session->db->fetchQueryAll($sql);
			for($i=0; $i<count($retArr); $i++ )
			{
				$msg.="[".($i+1)."]".$retArr[$i]['title']."\n";
			}
		}
		$msg.="</font>";
		$msg.= $this->getFooter($extr);
		return $msg;
	}
	
	public function getList($class_id,$count)
	{
		$msg = "<Font Face='楷体' Color='-16744320' Size='10.5'>";
		$extra = "";
		$perPage = 4;
		$pages = 1;
		$currentPage = $this->getPage($class_id);
		if($count>$perPage)
		{
			$pages = ceil($count/$perPage);
			$extra = ",(第{$currentPage}页 共{$pages}页)p-上一页 n-下一页";
			$start = $perPage * ($currentPage-1);
			$sql = "select * from fx_mindtest_questions where class_id=$class_id order by id desc limit $start, $perPage";
		}else 
		{
			$sql = "select * from fx_mindtest_questions where class_id=$class_id order by id desc";
		}
		
		$retArr = $this->session->db->fetchQueryAll($sql);
		for($i=0; $i<count($retArr); $i++ )
		{
			$msg.="[".($i+1)."] ".$retArr[$i]['title']."\n";
		}
		
		$msg.="</font>";
		$msg.= $this->getFooter($extra);
		
		$this->setQuestionID($class_id, 0);
		return $msg;
	}
	
	public function getTips()
	{
		$class_id=$this->extraParam['classid'];
		return $this->getList($class_id);
	}

	public function selectOption($class_id,$questionID,$input)
	{
		$msg = "<Font Face='楷体' Color='-16744320' Size='10.5'>";
		
		$sql = "select * from fx_mindtest_options where question_id={$questionID} order by id asc";
		$retArr = $this->session->db->fetchQueryAll($sql);
		$found = false;
		for($i=0; $i<count($retArr); $i++ )
		{
			if( intval($input)==($i+1))
			{
				$msg.= $retArr[$i]['answer']."\n";
				$found = true;
				break;
			}
		}
		if( $found )
		{
			$extra = ',l-列表';
		}else 
		{
			$msg.="您的输入不正确！";
		}
		$msg .= "</font>";
		return $msg.$this->getFooter($extra);
	}
	
	public function handle($input)
	{
		if( parent::handleInput($input)) return false;
					
		$class_id=$this->extraParam['classid'];
		
		$extra = "";
		$perPage = 4;
		$pages = 1;
		$currentPage = $this->getPage($class_id);
		
		$sql = "select count(*) as count from fx_mindtest_questions where class_id=$class_id order by id desc";
		$retArr = $this->session->db->fetchQueryAll($sql);
		$count = $retArr[0]['count'];
		
		if($count>$perPage)
		{
			$pages = ceil($count/$perPage);
		} 
					
		if( $input == 'p' )	//上一页
		{
			if($currentPage>1)
			{
				$currentPage--;
				$this->setPage($class_id, $currentPage);
			}
			return $this->getList($class_id,$count);
		}else if( $input == 'n') //下一页
		{
			if($currentPage<$pages)
			{
				$currentPage++;
				$this->setPage($class_id, $currentPage);
			}
			return $this->getList($class_id,$count);
		}else if($input == 'l')
		{
			return $this->getList($class_id,$count);
		}else if( is_numeric($input) )
		{
			$questionID = $this->getQuestionID($class_id);
			if( $questionID == 0 )
			{
				return $this->getListContent($class_id,$count,$input);
			}else
			{
				return $this->selectOption($class_id,$questionID,$input);
			}
		}
		return $this->getFooter();
	}
}
?>